package com.agile.leetcode.random;

import java.util.Random;

/**
 * @Author:ChenZhangKun
 * @Date: 2022/9/11 15:09
 */
public class FindSeedByStr {
    public static void main(String[] args) {
        System.out.println(generate("durant", Long.MIN_VALUE, Long.MAX_VALUE));
    }

    /**
     * 通过字符串寻找种子数
     *
     * @param goal
     * @param start
     * @param finish
     * @return
     */
    public static long generate(String goal, Long start, Long finish) {
        char[] input = goal.toCharArray();
        char[] pool = new char[input.length];
        label:
        for (long seed = start; seed < finish; seed++) {
            Random random = new Random(seed);
            for (int i = 0; i < input.length; i++)
                // 每个去匹配
                pool[i] = (char) ('`' + random.nextInt(27));
            if (random.nextInt(27) == 0) {
                // 此时结束循环，看看字符串是否匹配
                for (int j = 0; j < input.length; j++) {
                    if (input[j] != pool[j]) {
                        continue label;
                    }
                }
                return seed;
            }

        }
        throw new RuntimeException("没找到");
    }
}
